package com.technology.education.ui.student.message

import com.angcyo.dsladapter.findItemByTag
import com.angcyo.dsladapter.removeItem
import com.angcyo.dsladapter.toDslItemList
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.Message
import com.technology.education.ext.notifyDialog
import com.technology.education.ext.setPageItems
import com.technology.education.ext.toWebActivity
import me.hgj.jetpackmvvm.ext.parseState

/**
 * 消息列表
 */
class MessageFragment : BaseRecyclerTopBarFragment<MessageViewModel>() {
    override fun initViews() {
        setTitle("消息")
        noToolbarBack()
    }

    override fun lazyLoadData() {
        loading()
    }

    override fun createObserver() {
        mViewModel.delResult.observe(this) {
            renderAdapter {
                removeItem(it)
            }
        }

        mViewModel.readResult.observe(this) {
            renderAdapter {
                //通过payload通知消息更改已读状态
                findItemByTag(it)?.updateItemDependPayload(it)
            }
        }

        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { data ->
                renderAdapter {
                    hasMore(data.total)
                    setPageItems(
                        (data.list ?: listOf()).toDslItemList(R.layout.item_message) {
                            itemBindOverride = { itemHolder, _, _, payload ->
                                if (payload.isNotEmpty() && payload[0] == itemTag) {
                                    itemHolder.gone(R.id.iv_status)
                                }
                                itemData?.let { it ->
                                    if (it is Message) {
                                        itemTag = it.id
                                        itemHolder.tv(R.id.tv_title)?.text = it.title
                                        itemHolder.tv(R.id.tv_content)?.text = it.subhead
                                        itemHolder.tv(R.id.tv_date)?.text = it.createDate
                                        itemHolder.gone(R.id.iv_status, it.isRead)
                                        itemHolder.clickItem { _ ->
                                            mViewModel.readMessage(it.id)
                                            it.url?.let { url ->
                                                toWebActivity(it.title, url)
                                            }
                                        }
                                        itemHolder.longClickItem { _ ->
                                            notifyDialog(notify = "是否要删除该条消息?") {
                                                mViewModel.delMessage(it.id)
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        mViewModel.pageIndex
                    )
                }

            }, onError = {
                loadError()
            })
        }
    }
}